<%--
  Created by IntelliJ IDEA.
  User: CMCC
  Date: 2016/8/30
  Time: 16:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/contractInfo.css"/>
    <link rel="stylesheet" href="css/pagination.css"/>
    <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="layer/layer.js"></script>
    <script type="text/javascript" src="js/handlebars-v3.0.3.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/mui.min.js"></script>
    <script type="text/javascript" src="js/security.min.js"></script>
    <script type="text/javascript" src="js/pagination.js"></script>
    <script type="text/javascript">
        var hosturl = "http://"+ window.location.host + "/"+ window.location.pathname.split('/')[1];
        var adminSessionID = <%=session.getAttribute("SID")%>;
        var adminSessionName = "<%=session.getAttribute("SName")%>";
        if (adminSessionID === null) {
            window.location.href = hosturl+"/login.jsp";
        } else {
        }
    </script>
    <script type="text/javascript">
        window.onload = function () {
            var oUserInfo = document.getElementById('userInfo');
            var ocCntractInfo = document.getElementById('contractInfo');
            var oLogInfo = document.getElementById('logInfo');

            document.getElementById("adminName").innerHTML = " " + adminSessionName + " ";
            oUserInfo.onmousedown = function () {
                ocCntractInfo.style.backgroundColor = "#fff";
                ocCntractInfo.style.color = "#000";
                oUserInfo.style.backgroundColor = "#324e75";
                oUserInfo.style.color = "#fff";
            };
            ocCntractInfo.onmousedown = function () {
                oUserInfo.style.backgroundColor = "#fff";
                oUserInfo.style.color = "#000";
                ocCntractInfo.style.backgroundColor = "#324e75";
                ocCntractInfo.style.color = "#fff";
            }
            ocCntractInfo.onclick = function () {
                window.location.href = hosturl+'/contractMana.jsp';
            };
            oLogInfo.onclick = function () {
                window.location.href = hosturl+'/logMana.jsp';
            };
            //添加合同信息

            //注销
            var oLogout = document.getElementById('logout');
            oLogout.onclick = function () {
                mui.ajax(hosturl+"/admin/logout", {
                    data: {
//                    "contractId": 1
                    },
                    dataType: "json",
                    type: "get",
                    timeout: 10000,
                    success: function(data) {
                        alert(data.description)
                        window.location.href = hosturl+'/login.jsp';

                    },
                    error: function(xhr, type, errorThrown) {
                        console.log(type);
                    }
                });
            };

        }
    </script>
    <script type="text/javascript">
        mui.ready(function () {
            mui.ajax(hosturl+"/user/byAdmin", {
                data: {
//                    "": 1
                },
                dataType: "json",
                type: "post",
                timeout: 10000,
                success: function(data) {
                    document.getElementById("dataLen").innerHTML="共有"+data.body.length +"条";
                    $('#pagination-container').pagination({
                        dataSource: data.body,
                        pageSize: 5,
                        showGoInput: true,
                        showGoButton: true,
                        autoHidePrevious: true,
                        autoHideNext: true,
//                        showPrevious: false,
//                        showNext: false,
//                        showPageNumbers: false,
//                        showNavigator: true,
                        pageNumber: 1,
                        callback: function (data, pagination) {
                            // template method of yourself
                            var html = simpleTemplating(data);
                            $('#data-container').html(html);
                        }
                    });
                },
                error: function (xhr, type, errorThrown) {
                    console.log(type);
                }
            });
        });
    </script>

    <title>用户信息</title>
</head>

<body>
<div class="top">
    <div class="top-left">
        <p class="top-left">合同管理系统</p>
    </div>
    <div class="top-right">
        <p class="p1">欢迎管理员<span class="" id="adminName"> 张三 </span>的到来 <a href="#" id="logout">注销 </a></p>
    </div>
</div>
<div class="left-block">
    <ul>
        <li id="contractInfo" class="uleft-index">合同管理</li>
        <li id="userInfo" class="cleft-index">用户管理</li>
        <li id="logInfo" class="uleft-index">日志管理</li>
    </ul>
</div>
<div class="main-part">
    <ul>
        <li class="right-label">用户信息</li>
        <!--<li class=""><input class="right-btn" type="button" value="修改合同信息" /></li>-->
        <!--<li class=""><input class="right-btn" type="button" value="搜索" /></li>-->
        <li class=""><a href="http://183.224.40.151:8888/ContractMS/file/download?fileName=UserInfoTemplet.xls"><input class="right-btn" type="button" id="downloadBtn" value="下载用户资源模板"/></a>
        <li class=""><a href="javascript:addUser();"><input class="right-btn" type="button" id="addBtn" value="添加用户信息"/></a>
        </li>
        <li class="full-width"></li>
        <li class="">
            <div class="row">
                <div class="col-lg-3">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon1">用户名称</span>
                        <input id="uId" type="text" class="form-control" placeholder="名称"
                               aria-describedby="basic-addon1">
                    </div>
                </div>
                <div class="col-lg-3">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon2">用户姓名</span>
                        <input id="uName" type="text" class="form-control" placeholder="姓名"
                               aria-describedby="basic-addon1">
                    </div>
                </div>

                <div class="col-lg-3">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon5">用户部门</span>
                        <input id="uDepart" type="text" class="form-control" placeholder="用户部门"
                               aria-describedby="basic-addon1">
                    </div>
                </div>
                <div class="col-lg-3">
                    <button id="search" onclick="searchUser()" type="button" class="btn btn-primary">搜索</button>
                    <label id="dataLen">共有？条</label>
                    <input id="handle_status" value="" hidden="hidden">
                </div>
            </div>
        </li>
        <li>
            <table id="table1" class="table table-bordered table-striped table-hover">
                <thead>
                <tr>
                    <%--<th>编号</th>--%>
                    <th>用户名</th>
                    <%--<th>密码</th>--%>
                    <th>部门</th>
                    <th>OA邮箱</th>
                    <th>其他邮箱</th>
                    <th>姓名</th>
                    <th>电话</th>
                    <th>PS</th>
                    <th></th>
                </tr>
                </thead>
                <tbody id="data-container">
                </tbody>
            </table>
            <div id="wrapper">
                <section>
                    <div id="pagination-container"></div>
                </section>
            </div>
        </li>
    </ul>
</div>
</body>
<script type="text/javascript">

    //处理分页数据
    function simpleTemplating(data) {
        var html = '';
        $.each(data, function (index, item) {
            var PS = (item.primarySalt === null) ? " " : item.primarySalt;
            html += '<tr>';
//            html += '<td>'+ item.id +'</td>';
            html += '<td>' + item.userName + '</td>';
            html += '<td>' + item.department + '</td>';
            html += '<td>' + item.oa + '</td>';
            html += '<td>' + item.emailAddress + '</td>';
            html += '<td>' + item.name + '</td>';
            html += '<td>' + item.phone + '</td>';
            html += '<td>' + PS + '</td>';
//            html += '<td>'+ warnOrNor +'</td>';
            html += '<td><a href="javascript:updatePass(' + item.id + ');">修改密码</a>&nbsp;&nbsp;&nbsp;<a href="javascript:update(' + item.id + ');">修改</a>&nbsp;&nbsp;&nbsp;<a href="javascript:deleteUser(' + item.id + ',\'' + item.userName + '\');">删除</a></td>';
            html += '</tr>';
        });
        return html;

    };
    function searchUser() {
        var userName = $("#uId").val();
        var name = $("#uName").val();
        var department = $("#uDepart").val();
        mui.ajax(hosturl+"/user/reqByAdmin", {
            data: {
                'userName': userName,
                'name': name,
                'department': department
            },
            dataType: "json",
            type: "post",
            timeout: 10000,
            success: function (data) {
                if (data.status == "datanull") {
                    $('#data-container').html("");
                    $('#pagination-container').html("");;
                }else {
                    document.getElementById("dataLen").innerHTML="共有"+data.body.length +"条";
                    $('#pagination-container').pagination({
                        dataSource: data.body,
                        pageSize: 5,
                        showGoInput: true,
                        showGoButton: true,
                        autoHidePrevious: true,
                        autoHideNext: true,
//                        showPrevious: false,
//                        showNext: false,
//                        showPageNumbers: false,
//                        showNavigator: true,
                        pageNumber: 1,
                        callback: function (data, pagination) {
                            // template method of yourself
                            var html = simpleTemplating(data);
                            $('#data-container').html(html);
                        }
                    });
                }

            },
            error: function (xhr, type, errorThrown) {
                console.log(type);
            }
        });
    }

    function addUser() {
        layer.open({
            type: 2,
            title: '通过Excel导入用户信息',
            shadeClose: true,
            shade: [0.3, '#000'],
            area: ['543px', '343px'],
            content: hosturl+'/addUserByExcel.html',
            end: function(){
                var handle_status = $("#handle_status").val();
                if ( handle_status == 'OK' ) {
                    layer.msg('导入用户成功！',{
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    setTimeout(searchUser(),2000);
                } else if ( handle_status == 'none' ) {
                    layer.msg('您没有导入任何用户信息！',{
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    window.location.reload();
                }
            }
        });
    };
    function updatePass(userId) {
        layer.open({
            type: 2,
            title: '修改用户密码',
            shadeClose: true,
            shade: [0.3, '#000'],
            area: ['543px', '543px'],
            content: hosturl+'/updateUserPass.html?uid=' + userId,
            end: function () {
                var handle_status = $("#handle_status").val();
                if ( handle_status == 'OK' ) {
                    layer.msg('修改成功！',{
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    setTimeout(searchUser(),2000);
                } else if ( handle_status == 'FAIL' ) {
                    layer.msg('修改失败！',{
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    //window.location.reload();
                }
            }
        });
        layer.iframeAuto();
    };
    function update(userId) {
        layer.open({
            type: 2,
            title: '更新用户信息',
            shadeClose: true,
            shade: [0.3, '#000'],
            area: ['543px', '543px'],
            content: hosturl+'/updateUserInfo.html?uid=' + userId,
            end: function () {
                var handle_status = $("#handle_status").val();
                if ( handle_status == 'OK' ) {
                    layer.msg('修改成功！',{
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    setTimeout(searchUser(),2000);
                } else if ( handle_status == 'FAIL' ) {
                    layer.msg('修改失败！',{
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    //window.location.reload();
                }
            }
        });
    };

    function deleteUser(userId, userName) {
        if (confirm("确定要删除该用户吗？")) {
            var aj = $.ajax({
                url: hosturl+'/user/deleteByUserID',// 跳转到 action
                data: {
                    id: userId
                },
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    alert(data.description);
                    searchUser();
                },
                error: function () {
                    alert("异常！");
                }
            });
        }
        else {
        }
    };

    function downloadUserFile() {
        var fileName = "用户信息模板.xls";
        var aj = $.ajax({
            url: hosturl+'/file/download',// 跳转到 action
            data: {
                fileName: fileName
            },
            type: 'get',
            cache: false,
            dataType: 'json',
            success: function (data) {

            },
            error: function () {
                alert("异常！");
            }
        });
    }
</script>
</html>
